package com.shopping.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Set;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.shopping.dao.impl.BizDaoImpl;
import com.shopping.entity.TbGoods;
import com.shopping.service.impl.BizServiceImpl;

/**
 * Servlet implementation class CartAddServlet
 */
public class CartAddServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    HashMap<Integer, TbGoods> map = null;// 声明

    /**
     * @see HttpServlet#HttpServlet()
     */
    public CartAddServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {
        // TODO Auto-generated method stub
        map = new HashMap<Integer, TbGoods>();// 一次初始化多次调用
    }

    /**
     * @see Servlet#destroy()
     */
    public void destroy() {
        // TODO Auto-generated method stub
        super.destroy();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");

        BizDaoImpl bdi = new BizDaoImpl();
        BizServiceImpl bsi = new BizServiceImpl();
        bsi.setBizdao(bdi);// 注入

        PrintWriter out = response.getWriter();// 输出流
        HttpSession session = request.getSession();
        // Object obj = session.getAttribute("user");
        int id = Integer.valueOf(request.getParameter("id"));
        Object server_token = session.getAttribute("server_token");
        if (server_token != null) {

            String s_token = (String) server_token;
            String client_token = request.getParameter("client_token");
            // String uname = (String) obj;

            if (client_token.equals(s_token)) {
                /* if (obj != null) { */
                // 思考：先把登录的问题考虑进去，再考虑令牌
                // 正常跳转
                TbGoods tg = bsi.getShowGoods(id);
                // 思考:如何使用集合? 判断编号:1:重复的编号,数量累加 2:不包含编号的情况,新增一条
                if (map.containsKey(id)) {
                    // 1:重复的编号,数量累加
                    tg.setCount(map.get(id).getCount() + 1);
                    map.put(id, tg);
                } else {
                    // 2:不包含编号的情况,新增一条
                    map.put(id, tg);
                }
                double sum = 0;
                Set<Integer> set = map.keySet();
                for (Integer integer : set) {
                    if (map.get(integer).getFreePrice() > 0) {

                        sum += map.get(integer).getFreePrice() * (double) (map.get(integer).getCount());
                    } else {
                        sum += map.get(integer).getNowPrice() * (double) (map.get(integer).getCount());
                    }

                }

                session.setAttribute("sum", sum);
                session.setAttribute("map", map);
                // session.setAttribute("uname", uname);
                out.print("<script language='javascript'>alert('购买商品成功!');window.close();</script>");
                // request.getRequestDispatcher("show.jsp").forward(request,
                // response);
                /*
                 * } else { out.print("<script>");
                 * out.print("alert('您尚未登录，请先登录！');");
                 * out.print("window.location.href='init';");
                 * out.print("</script>"); out.flush(); out.close(); }
                 */
                // 消除session中的令牌

                session.removeAttribute("server_token");
            }
        } else {
            out.print("<script>");
            out.print("alert('请不要重复刷新!');");
            out.print("window.location.href='Init';");
            out.print("</script>");
            out.flush();
            out.close();

        }
    }
}
